package com.example.java_gobang.game;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

//管理用户在线的状态,如果会话存在则代表用户存在
@Component
public class OnlineUserManager {
    //用户 id 和会话的关系,gamehall 是游戏大厅，ganmebattle 是游戏对决。
    private ConcurrentHashMap<Integer, WebSocketSession> gamehall = new ConcurrentHashMap<>();
    private ConcurrentHashMap<Integer, WebSocketSession> gameroom = new ConcurrentHashMap<>();


    public void enterGameHall(int userId ,WebSocketSession webSocketSession){
        gamehall.put(userId,webSocketSession);
    }

    public void exitGameHall(int userid){
        gamehall.remove(userid);
    }

    public WebSocketSession getWebSocketSessionByUserIdInGameHall(int userid){
        return gamehall.get(userid);
    }


    public void enterGameRoom(int userId ,WebSocketSession webSocketSession){
        gameroom.put(userId,webSocketSession);
    }

    public void exitGameRoom(int userid){
        gameroom.remove(userid);
    }

    public WebSocketSession getWebSocketSessionByUserIdInGameRoom(int userid){
        return gameroom.get(userid);
    }
}
